<?php

Yii::import( "application.controllers.FrontendBaseController" );

class PetController extends FrontendBaseController
{

    public function actionIndex( $id = null )
    {
        if ( (!isset( $id )) || ($id == $this->getUserIdFromSession()) )
        {
            $isAuthor = true;
        }
        if ( !$isAuthor )
        {
            $user = UserFacade::getUserById( $id );
            $name_text = $user->name . " " . $user->surname;
            $post_text = "Любимцы";
            $name_url = "/user/" . $id;
        }
        else
        {
            $name_text = "Моя страница";
            $post_text = "Мои любимцы";
            $name_url = $this->getUserMainPageUrl();
        }
        $this->breadcrumbs = array( "Главная" => "/",
            $name_text => $this->getUserMainPageUrl( $id ),
            $post_text => "" );
        if ( !$isAuthor )
        {
            $dataProvider = new CActiveDataProvider( 'Pet', array(
                        'criteria' => array( 'condition' => 'user_id=' . $id )
                    ) );
        }
        else
        {
            $dataProvider = new CActiveDataProvider( 'Pet', array(
                        'criteria' => array( 'condition' => 'user_id=' . $this->getUserIdFromSession() )
                    ) );
        }

        $this->render( 'index', array(
            'dataProvider' => $dataProvider,
            'isAuthor' => $isAuthor,
                ) );
    }

    public function actionCreate()
    {
        $this->breadcrumbs = array( "Главная" => "/",
            "Моя страница" => $this->getUserMainPageUrl(),
            "Мои любимцы" => "/pet",
            "Добавление любимца" => "" );
        $model = new Pet;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if ( isset( $_POST[ 'Pet' ] ) )
        {
            $model->attributes = $_POST[ 'Pet' ];
            $model->user_id = $this->getUserIdFromSession();
            if ( PetFacade::savePet( $model ) )
                $this->redirect( array( 'view', 'id' => $model->pet_id ) );
        }

        $this->render( 'create', array(
            'model' => $model,
                ) );
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate( $id )
    {
        $model = $this->loadModel( $id );

        $this->breadcrumbs = array( "Главная" => "/",
            "Моя страница" => $this->getUserMainPageUrl(),
            "Мои любимцы" => "/pet",
            "&laquo;" . $model->nickname . "&raquo;" => $this->getPetPageUrl( $model->pet_id ),
            "Редактирование" => "" );

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if ( isset( $_POST[ 'Pet' ] ) )
        {
            print_r($_POST[ 'Pet' ]);
            $model->attributes = $_POST[ 'Pet' ];
            if ( PetFacade::savePet( $model ) )
            {
                $this->redirect( array( 'view', 'id' => $model->pet_id ) );
            }
        }

        $this->render( 'update', array(
            'model' => $model,
                ) );
    }

    public function actionUpdateAvatar( $id )
    {
        $model = $this->loadModel( $id );
        $this->breadcrumbs = array( "Главная" => "/",
            "Моя страница" => $this->getUserMainPageUrl(),
            "Мои любимцы" => "/pet",
            "&laquo;" . $model->nickname . "&raquo;" => $this->getPetPageUrl( $model->pet_id ),
            "Изменить фотографию" => "" );

        $this->render( 'updateAvatar', array( "pet" => $model ) );
    }

    public function actionDeleteAvatar( $id )
    {
        $model = $this->loadModel( $id );
        PetFacade::deletePetAvatar( $model );
        $this->redirect( "/pet/updateAvatar/" . $id );
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete( $id )
    {
        if ( Yii::app()->request->isPostRequest )
        {
            // we only allow deletion via POST request
            PetFacade::deletePet( $this->loadModel( $id ) );

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if ( !isset( $_GET[ 'ajax' ] ) )
                $this->redirect( isset( $_POST[ 'returnUrl' ] ) ? $_POST[ 'returnUrl' ] : array( 'admin' ) );
        }
        else
            throw new CHttpException( 400, 'Invalid request. Please do not repeat this request again.' );
    }

    public function loadModel( $id )
    {
        $model = PetFacade::loadModel( $id );
        if ( $model === null )
            throw new CHttpException( 404, 'The requested page does not exist.' );
        return $model;
    }

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView( $id )
    {
        $model = $this->loadModel( $id );

        $isAuthor = PetFacade::isAuthor( $model );
        if ( $isAuthor )
        {
            $user = $this->getUserFromSession();
            $nameText = "Моя страница";
            $petText = "Мои любимцы";
        }
        else
        {
            $user = $model->user;
            $nameText = $user->name . " " . $user->surname;
            $petText = "Любимцы";
        }
        $this->breadcrumbs = array( "Главная" => "/",
            $nameText => $this->getUserMainPageUrl( $model->user_id ),
            $petText => "/pet/" . $model->user_id,
            $model->nickname => "",
        );
        $this->render( 'view', array(
            'model' => $model,
            'isAuthor' => $isAuthor
                ) );
    }

    public function actionAll()
    {
        $model = new Pet;
        $model->unsetAttributes();  // clear any default values
        $this->registerAjaxSearch("pet-search", "pet-list");
        $this->breadcrumbs = array( "Главная" => "/",
            "Все любимцы" => "" );

        $nickname = $_GET['Pet']['nickname'];
        $type = $_GET['Pet']['type'];
        $race = $_GET['Pet']['race'];
        $dataProvider = PetFacade::getDataProviderForSearch($nickname, $type, $race);

        $this->render( 'all', array(
            'dataProvider' => $dataProvider,
            'searchModel' => $model
        ) );
    }

}